package com.gxa.eshops.sso.service.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.gxa.eshops.sso.service.SmsService;
import com.gxa.eshops.sso.util.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.TimeUnit;
/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: Mr.Wang
 * @create: 2022-07-06 09:47
 **/
@Service
@Transactional(rollbackFor = Throwable.class)
public class SmsServiceImpl implements SmsService {
    @Autowired
    private RedisTemplate<String ,Object> redisTemplate;

    public static final String LOGIN_SMS = "login_sms:";

    @Override
    public void sendLoginSms(String phone) throws ClientException {
        //生成验证码,用工具类的静态方法调用
       String sysCode = SendSms.getMsgCode();
        System.out.println("sysCode:"+sysCode);
        //服务器存储验证码15分钟
        redisTemplate.opsForValue().set(LOGIN_SMS+phone,sysCode,15, TimeUnit.MINUTES);
        //调用工具类发送短信给用户
        String templateParam = "{\"code\":"+sysCode+"}";
        SendSms.sendSms(phone,sysCode,templateParam);
        //回调
        /*SendSmsResponse response = SendSms.sendSms(phone, "SMS_204986028", templateParam);
        System.out.println("短信接口返回的数据:");
        System.out.println("Code:"+response.getCode());
        //ok成功
        System.out.println("Message:"+response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());*/
    }
}
